package com.taotao.portal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.pojo.TaotaoResult;
import com.taotao.portal.pojo.SearchItem;
import com.taotao.portal.pojo.SearchResult;
import com.taotao.portal.service.SearchService;
import com.taotao.util.HttpClientUtil;
import com.taotao.util.JsonUtils;

@Service
public class SearchServiceImpl implements SearchService {

	@Value("${SEARCH_BASE_URL}")
	private String SEARCH_BASE_URL;

	@Override
	public SearchResult search(String query, Integer currentPage, Integer rows) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("keyword", query);
		param.put("currentPage", currentPage.toString());
		param.put("rows", rows.toString());
		// 返回的是TaotaoResult json 需要转换
		String json = HttpClientUtil.doGet(SEARCH_BASE_URL, param);
		// formatToPojo这个方法是设置TaotaoResult的date数据类型的
		TaotaoResult toPojo = TaotaoResult.formatToPojo(json,
				SearchResult.class);
		// 取返回的结果
		SearchResult searchResult = (SearchResult) toPojo.getData();

		return searchResult;
	}

}
